package com.soft.controller;

import com.soft.entity.*;
import com.soft.service.CarInfoService;
import com.soft.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;

@Controller
@RequestMapping("/CarInfo")
public class CarInfoController {

    @Autowired
    private CarInfoService carInfoService;

    @RequestMapping("getAllCarInfoList")
    @ResponseBody
    public ResultUtil getAllCarInfoList(Integer page, Integer limit,CarInfoSearch search) {
        return carInfoService.getAllCarInfoList(page, limit,search);
    }

    @RequestMapping("CarInfoList")
    public String carInfoList() {
        return "jsp/Bus/CarInfoList";
    }


    @RequestMapping("addCarInfo")
    public String CarInfoAdd(){
        return "jsp/Bus/addCarInfo";
    }

    @RequestMapping("insertCarInfo")
    @ResponseBody
    public ResultUtil insCarInfo(CarInfo carInfo){
        //防止浏览器提交
        CarInfo carInfo1 = carInfoService.selCarInfoBynumber(carInfo.getNumber());
        if(null != carInfo1){
            return new ResultUtil(500,"车牌号已存在，请重新填写！");
        }
        try {
            carInfoService.insertCarInfo(carInfo);
            return ResultUtil.ok();
        } catch (Exception e) {
            return new ResultUtil(502,"网络错误，请检查网络！");
        }
    }

    @RequestMapping("deleteCarInfoById")
    @ResponseBody
    public ResultUtil deleteCarInfoById(Integer carId) {
        return carInfoService.deleteCarInfoById(carId);
    }


    @RequestMapping("editCarInfo/{carId}")
    public String editCarInfo(@PathVariable("carId") Integer carId, HttpSession session) {
        CarInfo carInfo = carInfoService.getCarInfoById(carId);
        session.setAttribute("CarInfo", carInfo);
        return "jsp/Bus/editCarInfo";
    }


    @RequestMapping("updateCarInfo")
    @ResponseBody
    public ResultUtil updateCarInfo(CarInfo carInfo) throws ParseException {
        return carInfoService.updateCarInfo(carInfo);
    }

}
